﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace OgrIsleri.DashBoard.Modul.Ogrenci
{
    public partial class EkleGuncelle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Request.QueryString["Goster"] != null)
                ProfilGetir();
        }

        private void ProfilGetir()
        {
            using (DbEntities db = new DbEntities())
            {
                Guid userID = new Guid(Request.QueryString["Goster"]);
                var data = db.tblUyeDetaylaris.Where(o => o.UserId == userID).SingleOrDefault();
                lblAdSoyad.Text = data.AdSoyad;
                lblDogumTarihi.Text = ((DateTime)(data.DogumTarihi)).ToShortDateString();
                lblCinsiyet.Text = data.Cinsiyet == 1 ? "Erkek" : "Kadın";
                lblSinif.Text = data.tblSiniflar.SinifAdi;
                lblCepTel.Text = data.CepTel;
                lblSabitTel.Text = data.SabitTel;
                lblSehir.Text = data.tblIlceler.tblSehirler.SehirAdi;
                lblIlce.Text = data.tblIlceler.IlceAdi;
                ltrAdres.Text = data.Adres;
                lblKullaniciAdi.Text = data.aspnet_Users.UserName.ToString();
                lblMail.Text = data.aspnet_Users.aspnet_Membership.Email;
                Valid2.Enabled = false;

            }
        }


        protected void BtnKaydet_Click(object sender, EventArgs e)
        {
            using (DbEntities db = new DbEntities())
            {
                if (Request.QueryString["Goster"] != null)
                {
                    MembershipUser Kullanici = Membership.GetUser(lblKullaniciAdi.Text);
                    

                    Guid userID = new Guid(Request.QueryString["Goster"]);
                    var data = db.tblUyeDetaylaris.Where(o => o.UserId == userID).SingleOrDefault();

                    var sorgu = db.aspnet_Membership.Where(o => o.UserId != (Guid)Kullanici.ProviderUserKey && o.Email == lblMail.Text).ToList();

                    if (sorgu.Count <= 0)
                    {

                        Kullanici.Email = lblMail.Text;
                        Membership.UpdateUser(Kullanici);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "Hata", "alert('E-posta kullanımda')", true);
                        return;
                    }

                    if (!string.IsNullOrWhiteSpace(txtSifre.Text))
                    {
                        if (txtSifre.Text.Equals(txtSifreOnay.Text))
                        {
                            string EskiSifre = Kullanici.ResetPassword();
                            Kullanici.ChangePassword(EskiSifre, txtSifre.Text);
                            Response.Redirect("/DashBoard/");
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "Hata", "alert('Şifreler uyumlu değil')", true);
                        }
                    }
                    else
                    {
                        Response.Redirect("/DashBoard/");
                    }

                }
               
            }
        }

    }
}